package cn.jiedh.app.meiweidk.msg;

import android.os.Bundle;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import org.xutils.ex.HttpException;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import butterknife.BindView;
import cn.jiedh.app.meiweidk.MyWebActivity;
import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.ActivityUtil;
import cn.jiedh.app.meiweidk.Utils.tool.HtmlUtil;
import cn.jiedh.app.meiweidk.Utils.tool.L;
import cn.jiedh.app.meiweidk.Utils.tool.T;
import cn.jiedh.app.meiweidk.Utils.tool.UrlUtils;
import cn.jiedh.app.meiweidk.base.BaseActivity;
import cn.jiedh.app.meiweidk.entity.MsgInfoBean;
import cn.jiedh.app.meiweidk.network.ResponseFailure;
import cn.jiedh.app.meiweidk.network.ResponseSuccess;
import cn.jiedh.app.meiweidk.response.BaseResponse;

public class MsgDetailActivity extends BaseActivity {

    @BindView(R.id.bar_tv_title)
    TextView title;
    int messageId;//消息ID
    MsgInfoBean msgInfoBean;
    @BindView(R.id.empty_nn_pic)
    ImageView emptyNnPic;
    @BindView(R.id.empty_nn_btn)
    TextView emptyNnBtn;
    @BindView(R.id.web)
    WebView web;
    @BindView(R.id.title)
    TextView msgtitle;
    @BindView(R.id.time)
    TextView time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_msg_detail;
    }

    @Override
    protected void initView(View view) {
        title.setText("正在跳转...");
        setBack(true);
    }

    @Override
    protected void initData() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            messageId = bundle.getInt("messageId");
        }
        Map map = new HashMap<String, Objects>();
        map.put("messageId", messageId);//消息ID
        request(this, UrlUtils.MSGDETAIL, map, false, new ResponseSuccess<MsgInfoBean>() {
            @Override
            public void onRequestSucceed(BaseResponse<MsgInfoBean> response) {
                if (response.getCode() == 0) {
                    msgInfoBean = response.getData();
                    Bundle bundle = new Bundle();
                    if(msgInfoBean.getTitle()==null){
                        bundle.putString("title", title.getText().toString());
                    }else{
                        bundle.putString("title", msgInfoBean.getTitle());
                    }

                    bundle.putInt("id", msgInfoBean.getMessageId());
                    bundle.putInt("source", 3);
                    if (msgInfoBean.getMessageType() == 0) {//0链接，1富文本
                        bundle.putInt("type", 0);
                        bundle.putString("url", msgInfoBean.getLink());
                        ActivityUtil.next(MsgDetailActivity.this, MyWebActivity.class, bundle);
                        finish();
                    } else {
//                        bundle.putInt("type", 1);
//                        bundle.putString("content", msgInfoBean.getText());
                        //
                        title.setText(getResources().getString(R.string.msg_title));
                        msgtitle.setText(msgInfoBean.getTitle());
                        time.setText(msgInfoBean.getStartTime());
                        web.loadData(msgInfoBean.getText(), HtmlUtil.MIME_TYPE, HtmlUtil.ENCODING);
                    }

                } else {
                    title.setText(getResources().getString(R.string.msg_title));
                    findViewById(R.id.msg_detail_empty).setVisibility(View.VISIBLE);
                    findViewById(R.id.web).setVisibility(View.GONE);
                    emptyNnPic.setImageResource(R.mipmap.nodate);
                    emptyNnBtn.setText("这条消息飞走了");
                    T.showShortToast(response.getMessage());
                }
            }
        }, new ResponseFailure() {
            @Override
            public void onRequestFailure(HttpException error, String msg) {
                L.e("初始化banner错误" + msg);
                title.setText(getResources().getString(R.string.msg_title));
                T.showShortToast(msg);
            }
        });
    }
}
